#!/bin/sh

### This script mounts a sharepoint of your choice for rsync backups of your home folder ###
### Created on 11/3/2009 by Andrew Caldwell ###

###########################
# DEFINE GLOBAL VARIABLES #
###########################

currentDate=`date +%m-%d-%y`				# Set the current date
mountPoint=""								# Where you would like to mount the shared drive
sharePoint=""								# The name of the shared drive you'd like to mount
fileServer=""								# Name of the fileserver containing the share you'd like to mount
exitCode=0									# Keeps track of the exit code of functions, 0 is no error
remUsername=""								# Username to access the share
remPassword=""								# Password to access the share
locUsername=whoami							# Local Username
locDirectory=""								# Source directory for backup
setPreferences=""							# Save the configuration settings for next time
rsyncSwitch=""								# options for rsync

####################
# DEFINE FUNCTIONS #
####################

function getParameters ()
{
	rm ~/Library/Preferences/com.acdesigntech.ACBackup.txt
	
	echo "Please enter the share point: "
	read sharePoint
	echo "Please enter the mountPoint: "
	read mountPoint
	echo "Please enter the name/address of the fileserver: "
	read fileServer
	echo "Please enter a username for this share: "
	read remUsername
	echo "Please enter a password for this share: "
	read remPassword
	echo "Please enter the directory you'd like to back up (default is your home directory): " #single word only plz. Script can't handle multiple arguments yet.
	read locDirectory

	if [ -z $locDirectory ]; then
		locDirectory="~"
	fi

	mkfile -n 1k ~/Library/Preferences/com.acdesigntech.ACBackup.txt
	
		echo $sharePoint >> ~/Library/Preferences/com.acdesigntech.ACBackup.txt
		echo $mountPoint >> ~/Library/Preferences/com.acdesigntech.ACBackup.txt
		echo $fileServer >> ~/Library/Preferences/com.acdesigntech.ACBackup.txt
		echo $remUsername >> ~/Library/Preferences/com.acdesigntech.ACBackup.txt
		echo $remPassword >> ~/Library/Preferences/com.acdesigntech.ACBackup.txt
		echo $locDirectory >> ~/Library/Preferences/com.acdesigntech.ACBackup.txt
		logFile
	#else 
	#	echo "Unable to create file ~/Library/Preferences/com.acdesigntech.ACBackup.txt. " 
	#	echo "Unable to save settings. Check permissions and try again. Exiting program."
	#	exitCode=3
	#fi
	
	
}

function readParameters ()
{
	
	old_IFS=$IFS
	IFS=$'\n'
	cd ~/Library/Preferences
	
	configFile=($(cat com.acdesigntech.ACBackup.txt))
	
	IFS=$old_IFS
	
	echo "Parameters are as follows:\n"
	for i in {0..5}
	do
		echo ${configFile[i]}
	done
	echo "Accept these settings? (y/n)"
	read settingsAccept
	
	if [ -z $settingsAccept ] || [ $settingsAccept == "y" ] || [ $settingsAccept == "Y" ]; then
		sharePoint=${configFile[0]}
		mountPoint=${configFile[1]}
		fileServer=${configFile[2]}
		remUsername=${configFile[3]}
		remPassword=${configFile[4]}
		locDirectory=${configFile[5]}
		logFile
	else 
		getParameters
	fi
	
}

function logFile ()
{
	mkdir -p ~/Library/Logs/AC_BU_Logs	# create a directory to hold the logs if not already there
		date +%m-%d-%y >> ~/Library/Logs/AC_BU_Logs/$currentDate.log			# begin writing the log
		echo "\n" >> ~/Library/Logs/AC_BU_Logs/$currentDate.log
		date +%H:%M:%S >> ~/Library/Logs/AC_BU_Logs/$currentDate.log
		echo "\n" >> ~/Library/Logs/AC_BU_Logs/$currentDate.log
		echo "--------\n" >> ~/Library/Logs/AC_BU_Logs/$currentDate.log

		open ~/Library/Logs/AC_BU_Logs/$currentDate.log							# open the current log file
	
		mountNetworkVolume
	#else
	#	echo "Unable to create directory ~/Library/Logs/AC_BU_Logs. Check permissions and try again. Exiting program." 
	#	exitCode=2
	#fi
}

function mountNetworkVolume ()
{
	mkdir -p /Volumes/$mountPoint
	mount_afp afp://$remUsername:$remPassword@$fileServer/$sharePoint /Volumes/$mountPoint
	mountedVolume=`df | grep $mountPoint`

	if [ "$mountedVolume" != "" ]; then
		echo "Volume $sharePoint mounted successfully. Beginning rsync." >> ~/Library/Logs/AC_BU_Logs/$currentDate.log
		syncServices
	else
		echo "Unable to mount volume $sharePoint. Check the username, password, and server address and try again. Exiting program" >> ~/Library/Logs/AC_BU_Logs/$currentDate.log
		exitCode=1
	fi
}

function syncServices ()
{
	rsync -avE --exclude=*/Library/Caches/* --exclude=*vmware* $locDirectory /Volumes/$mountPoint >> ~/Library/Logs/AC_BU_Logs/$currentDate.log
	echo "rsync finished. Ejecting network disk in 30 seconds" >> ~/Library/Logs/AC_BU_Logs/$currentDate.log
	sleep 30
	diskutil unmount /Volumes/$mountPoint
	mountedVolume=`df | grep $mountPoint`

	if [ "$mountedVolume" == "" ]; then
		echo ""
	else
		echo "Unable to eject $mountPoint. rsync successful. Try manually unmounting. Exiting program." >> ~/Library/Logs/AC_BU_Logs/$currentDate.log
		exitCode=4
	fi
}
	
######################
# BEGIN MAIN PROGRAM #
######################

if [ -e ~/Library/Preferences/com.acdesigntech.ACBackup.txt ]; then
	readParameters	
else
	getParameters
fi

case $exitCode in
	1)
		echo "Error in function mountNetworkVolume()" >> ~/Library/Logs/AC_BU_Logs/$currentDate.log
		;;
	2) 
		echo "Error in function logFile()" >> ~/Library/Logs/AC_BU_Logs/$currentDate.log
		;;
	3) 
		echo "Error in function getParameters()" >> ~/Library/Logs/AC_BU_Logs/$currentDate.log
		;;
	4) 
		echo "Warning in function syncServices()" >> ~/Library/Logs/AC_BU_Logs/$currentDate.log
		;;
	0) echo "Program completed successfully" >> ~/Library/Logs/AC_BU_Logs/$currentDate.log
		;;
esac

exit $exitCode

